package com.danger.Presenter;

import android.content.Context;

import com.danger.Model.action.ModifyModel;
import com.danger.Model.action.ModifyModelImpl;
import com.danger.Model.bean.ObjectInfo;
import com.danger.Model.listener.OnUpdateListener;
import com.danger.Model.listener.OnUploadPhotoListener;
import com.danger.View.ModifyView;

/**
 * Created by danger on 2016/3/31.
 */
public class ModifyPresenterImpl implements ModifyPresenter {

    private Context context;
    private ModifyModel modifyModel;
    private ModifyView modifyView;

    public ModifyPresenterImpl(Context context, ModifyView modifyView) {
        this.context = context;
        this.modifyView = modifyView;
        modifyModel = new ModifyModelImpl(context);
    }

    @Override
    public void modify() {
        ObjectInfo objectInfo = modifyView.getObjectInfo();
        String result = modifyModel.check(objectInfo);
        if (result.equals("正确")) {
            modifyView.showDialog();
            modifyModel.modify(objectInfo, new OnUpdateListener() {
                @Override
                public void onSuccess() {
                    modifyView.hideDialog();
                    modifyView.showSuccess("修改成功", null);
                    modifyView.back();
                }

                @Override
                public void onError(String s) {
                    modifyView.hideDialog();
                    modifyView.showError(s);
                }
            });
        } else {
            modifyView.showError(result);
        }
    }

    @Override
    public void uploadPhoto(String path) {
        modifyView.showDialog();
        modifyModel.uploadPhoto(path, new OnUploadPhotoListener() {
            @Override
            public void onSuccess(String url) {
                modifyView.hideDialog();
                modifyView.showSuccess("上传成功", url);
            }

            @Override
            public void onError(String s) {
                modifyView.hideDialog();
                modifyView.showError(s);
            }
        });
    }
}
